/*package com.ym.b2b.ws.service.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.buyer.dao.EmCBuyerDao;
import com.ym.b2b.buyer.sqlbean.EmCBuyer;
import com.ym.b2b.buyer.sqlbean.SimpleEmCBuyer;
import com.ym.b2b.newylorder.service.OrderService;
import com.ym.b2b.newylorderplan.dao.OrderPlanDao;
import com.ym.b2b.newylorderplan.dao.OrderPlanDetailDao;
import com.ym.b2b.newylorderplan.sqlbean.OrderPlanDetailIn;
import com.ym.b2b.newylorderplan.sqlbean.OrderPlanIn;
import com.ym.b2b.newylshoppingcart.dao.ShoppingCartDao;
import com.ym.b2b.newylshoppingcart.sqlbean.NewYLshoppingCart;
import com.ym.b2b.order.sqlbean.EmSShoppingCart;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.returnModel.SendMessage;
import com.ym.b2b.ws.base.BaseWs;
import com.ym.b2b.ws.service.NewYLService;

@Service
@WebService
public class NewYLServiceImpl extends BaseWs implements NewYLService {
	@Autowired
	private EmCBuyerDao 				emCBuyerDao;
	@Autowired
	private OrderPlanDao 				ylorderPlanDao;
	@Autowired
	private OrderPlanDetailDao 			ylorderPlanDetailDao;
	@Autowired
	private ShoppingCartDao 			ylshoppingCartDao;
    @Autowired
    private ProductDao     				productDao;
    @Autowired
    private OrderService     			OrderService;
    
	public String addToCart(String INJSON) {
		logger.info("开始调用webService方法addToCart,向购物车及采购计划单添加数据");
		logger.info("INJSON:" + INJSON);
		SendMessage sendMessage = new SendMessage();
		try {
			JSONArray array = JSONArray.fromObject(INJSON);
			Object[] ob = array.toArray();
			if (ob.length == 0) {
				sendMessage.setMessage("101");
				sendMessage.setInfo("无效的采购计划单");
				return JSON.toJSONString(sendMessage);
			}

			// 遍历客户/采购计划单
			for (int i = 0; i < ob.length; i++) {
				JSONObject obj = JSONObject.fromObject(ob[i]);
				OrderPlanIn opIn = (OrderPlanIn) JSONObject.toBean(obj,OrderPlanIn.class);
				
				// 判断采购计划单是否已经存在
				Long count = ylorderPlanDao.count(opIn);
				if (count.longValue() > 0L) {
					logger.info("重复发送的采购计划单,采购计划单已经保存");
					sendMessage.setInfo("操作成功");
					return JSON.toJSONString(sendMessage);
				}
				// 判断客户采购商是否存在,避免数据异常
				String partyid = opIn.getPartyid();
				if ((partyid == null) || ("".equals(partyid))) {
					sendMessage.setMessage("103");
					sendMessage.setInfo("JSON数据异常,第" + i + "条采购计划没有采购商ID:partyid");
					return JSON.toJSONString(sendMessage);
				}
				EmCBuyer emCBuyer = new EmCBuyer();
				emCBuyer.setBuyerCode(partyid);
				SimpleEmCBuyer buyer = emCBuyerDao.showSimpleEmCBuyerDetail(emCBuyer);
				if (buyer == null) {
					sendMessage.setMessage("124");
					sendMessage.setInfo("JSON数据异常,第" + i + "条采购计划不存在该客户");
					return JSON.toJSONString(sendMessage);
				}
				// 收货地址转化判断
				String deliverySite = opIn.getDeliverySite();
				if ((deliverySite == null) || ("".equals(deliverySite.trim()))) {
					sendMessage.setMessage("103");
					sendMessage.setInfo("JSON数据异常,第" + i + "条采购计划没有收货地址:deliverySite");
					return JSON.toJSONString(sendMessage);
				}
				// 折扣金额转化判断
				String allDiscountMoney = opIn.getAllDiscountMoney();
				if ((allDiscountMoney == null)|| ("".equals(allDiscountMoney.trim()))) {
					opIn.setAllDiscountMoney("0");
				} else if (!isMoney(allDiscountMoney)) {
					sendMessage.setMessage("103");
					sendMessage.setInfo("JSON数据异常,第" + i + "条采购计划采购卷总金额allDiscountMoney不是有效值");
					return JSON.toJSONString(sendMessage);
				}
				
				// 保存采购计划单----------------------------------------------------
				ylorderPlanDao.insertOP(opIn);
				
				// 保存newyl购物车===================================================
				// 保存采购计划单详情〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
				NewYLshoppingCart shoppingCart = new NewYLshoppingCart();
				shoppingCart.setOrderplanid(opIn.getPlanid());
				shoppingCart.setBuyerCode(partyid);
				
				EmSShoppingCart emsShoppingCart = new EmSShoppingCart();
				emsShoppingCart.setBuyerCode(partyid);
				
				JSONArray array2 = JSONArray.fromObject(opIn.getMedicines());
				Object[] ob2 = array2.toArray(); 
				// 遍历产品
				for(int j = 0; j<ob2.length; j++){
					// 将json中Medicines中内容转为OrderPlanDetailIn
					JSONObject obj2 = JSONObject.fromObject(ob2[j]);
					OrderPlanDetailIn opDeatail = (OrderPlanDetailIn)obj2.toBean(obj2,OrderPlanDetailIn.class);
					opDeatail.setOrderplanid(opIn.getPlanid());
					
					// 优惠券金额处理
					String discountMoney = opDeatail.getDiscountMoney(); // 该产品采购卷单价 2014-10-10添加
					if (discountMoney == null || "".equals(discountMoney.trim())) {
						discountMoney = "0";
					}else if (!isMoney(discountMoney)) {
						sendMessage.setMessage("103");
						sendMessage.setInfo("JSON数据异常,第"+ i +"条采购计划单第"+ j + "个商品采购卷单价异常不是有效值");
						return JSON.toJSONString(sendMessage);
					}
					// 判断产品是否存在
					Integer productCount = productDao.hasProduct(opDeatail.getMcode());
					if (productCount.longValue() <= 0L) {
						sendMessage.setMessage("123");
						sendMessage.setInfo(opDeatail.getMcode()); // 第"+ i +"条采购计划单第"+ j + "个商品不存在
						return JSON.toJSONString(sendMessage);
					}
					ylorderPlanDetailDao.insertOPdetail(opDeatail);
					
					shoppingCart.setAmount(Double.valueOf(opDeatail.getQuantity()));
					shoppingCart.setProductCode(opDeatail.getMcode());
					ylshoppingCartDao.insert(shoppingCart);
					
					emsShoppingCart.setAmount(Double.valueOf(opDeatail.getQuantity()));
					emsShoppingCart.setProductCode(opDeatail.getMcode());
					EmSShoppingCart shoppingCartForDel = new EmSShoppingCart();
					shoppingCartForDel.setProductCode(opDeatail.getMcode());
					shoppingCartForDel.setBuyerCode(partyid);
					emShoppingCartDao.deleteShoppingCart(shoppingCartForDel); // 先删再加,确保不重复
					emShoppingCartDao.addShoppingCart(emsShoppingCart);
				}
				
			}
			sendMessage.setInfo("操作成功");
			return JSON.toJSONString(sendMessage);
		} catch (Exception e) {
			logger.error("",e);
			sendMessage.setMessage("11");
			sendMessage.setInfo("操作失败");
		}
		return JSON.toJSONString(sendMessage);
	}

	private static boolean isMoney(String str) {
		if ((str == null) || (str.equals(""))) {
			return false;
		}
		Pattern pattern = Pattern.compile("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	@Override
	public String confirmOrder(String orderCode, String buyerCode) {
		logger.info("开始调用webService方法confirmOrder,取消订单");
		logger.info("orderCode: " + orderCode + "buyerCode: " + buyerCode);
		return OrderService.cancelOrder(orderCode, buyerCode);
	}
}*/